<?php

/**
 * Description of NewslettersController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Newsletters\Backend\Controller;

if( !defined('IN') ) die('Hacking Attempt');

use Library\BackController;
use Library\HttpRequest;
use Library\Classe\Form\Form;

class NewslettersController extends BackController{
    //put your code here
    
    public function executeList(HttpRequest $request){
     
        $this->page->addVar('title', 'Gestion Newsletter');
        
        $manager = $this->managers->getManagerOf('Newsletters');
        
        $newsletters = $manager->getNewsletters();
        
        $this->page->addVar('newsletterslist', $newsletters);
        
        $this->leftcolumn();
        $this->rightcolumn();
    }
    
    private function leftcolumn(){
        $out = array();
        $out['add-newsletter.html']= 'Envoyer une newsletter';
        $out['newsletter.html']= 'Liste';
        $out['newsletter-membre.html']= 'Membre';
        
        return $this->page->addVar('left_content', $out);
        
    }
    private function rightcolumn(){
        $out ='Gérez vos newsletters. Vous pouvez ajouter ou lire une newsletter.';
        return $this->page->addVar('right_content', $out);
    }
    
    public function executeDelete(HttpRequest $request){
        
        $manager = $this->managers->getManagerOf('Newsletters');
        if($request->getExists('id')){
            $out['id'] = $request->getData('id');
            if($manager->deleteNewsletters($out)){
                $this->errors = 'suppression réussie';
            }else{
                $this->errors = 'Echec lors de la suppression';
            }
            $this->app()->httpResponse()->redirect('newsletter.html');
            
        }
    }
    
    public function executeCreate(HttpRequest $request){
        $this->page->addVar('title', 'Envoyer une newsletter');
        $this->leftcolumn();
        $this->rightcolumn();
        $manager = $this->managers->getManagerOf('Newsletters');
        
        $dataForm = new Form('addNewsletters');
        $dataForm->action($request->requestURI());
        //ajout des champs
        $dataForm->add('Text', 'titre')
                ->label('Titre')
                ->required(true);
        
        $dataForm->add('Textarea', 'message')
                ->label('Message')
                ->add_class('field_text')
                ;
                
        if($request->getExists('id')){
            $dataForm->add('Hidden', 'id')->value($request->getData('id'));
        }
        $dataForm->add('Submit', 'submit')->value('Envoyer');
        if($request->getMethod('post')){
           //test de la validation du post
            if ($dataForm->is_valid($_POST)) {
                list($titre, $texte) = $dataForm->get_cleaned_data('titre','message');
                $_POST['message'] = htmlspecialchars($texte);
                $_POST['dateNews'] = date('Y-m-d h:i:s');
                $_POST['titre'] = strip_tags($titre);
                $_POST['nbr'] = intval(count($manager->getMembres()));
                if(!$request->getExists('id')){
                    if($manager->addNewsletters($_POST)){
                        $this->sendNewletter($_POST);
                        $this->app()->httpResponse()->redirect('newsletter.html');
                    }else{
                        $this->errors = 'Echec lors de l\'enregistrement';
                    }
                }else{
                    if($manager->updateNewsletters($_POST)){
                        $this->sendNewletter($_POST);
                        $this->app()->httpResponse()->redirect('newsletter.html');
                    }else{
                        $this->errors = 'Echec lors de la mise à jour';
                    }
                }
            }else{
                $dataForm->bound($_POST);
            }
        }else{
            if($request->getExists('id')){
               $data = $manager->getNewslettersById(intval($request->getData('id')));
               $data_array['titre'] = $data->getTitre();
               $data_array['message'] = html_entity_decode($data->getMessage());
               $dataForm->bound($data_array);
            }else{
                $dataForm->bound($_POST);
            }
        }
        $this->page->addVar('errors', $this->errors);
        $this->page->addVar('dataForm', $dataForm);  
    }
    
    public function executeMember(HttpRequest $request){
        $this->page->addVar('title', 'Inscrit à la newsletter');
        $this->leftcolumn();
        $this->rightcolumn();
        $manager = $this->managers->getManagerOf('Newsletters');
        $members = $manager->getMembres();
        $this->page->addVar('members', $members);
    }
    public function sendNewletter(array $param){
        
        $manager = $this->managers->getManagerOf('Newsletters');
        $result = $manager->getMembres();
        foreach ($result as $data) {
            mail($data['email'], $param['titre'], $param['message']);
        }
    }
}

?>
